<template>
<div class="login">
    <div class="login-imgs">
        <img :src="require(`@/assets/img/login2.png`)" alt="">
    </div>
    <el-card>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
            <el-form-item>
                <div class="head-title">机器学习平台</div>
            </el-form-item>
            <el-form-item prop="username">
                <el-input auto-complete="off" clearable v-model="ruleForm.username">
                    <span slot="prefix">账号：</span>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input auto-complete="off" clearable type="password" v-model="ruleForm.password">
                    <span slot="prefix">密码：</span>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm" v-preventReClick="2000">登 录</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</div>
</template>

<script>
export default {
    name: 'login',
    data () {
        return {
            ruleForm: {
                username: null,
                password: null
            },
            rules: {
                username: [{
                    required: true,
                    message: '请输入用户名',
                    trigger: 'blur'
                }],
                password: [{
                    required: true,
                    message: '请输入密码',
                    trigger: 'blur'
                }]
            }
        }
    },
    created () {
        this.handleEnter(true)
    },
    destroyed () {
        this.handleEnter(false)
    },
    methods: {
        handleEnter (flag) {
            document.onkeydown = e => {
                if (window.event.keyCode === 13) {
                    if (flag) {
                        this.submitForm()
                    }
                }
            }
        },
        submitForm () {
            this.$refs['ruleForm'].validate(valid => {
                if (valid) {
                    this.$formData('/login', this.ruleForm).then(res => {
                        if (res) {
                            this.$message.success('登录成功')
                            localStorage.setItem('USER_TOKEN', JSON.stringify(res.token))
                            localStorage.setItem('PERMISSIONS', JSON.stringify(res.permissions))
                            localStorage.setItem('ROLES', JSON.stringify(res.roles))
                            localStorage.setItem('USER', JSON.stringify(res.user))
                            this.$router.push('/home')
                        }
                    })
                } else {
                    return false
                }
            })
        }
    }
}
</script>

<style lang="less">
.login {
    position: absolute;
    overflow: auto;
    left: 0;
    top: 0;
    width: 100%;
    min-width: 1024px;
    height: 100%;
    background: -webkit-gradient(linear, left top, right top, from(#00253B), to(#001A2E));
    .login-imgs {
        width: 60%;
        height: 100%;
        background: url('../../assets/img/login1.png') no-repeat center;
        background-size: 80% 80%;
        img {
            width: 100%;
            height: 100%;
            animation:dianAnimation 3s infinite;
        }
    }
    @keyframes dianAnimation
        {
            // 0% {opacity: 1;}
            // 5% {opacity: 0;}
            // 10% {opacity: 0; transform: rotate(0deg)}
            // 50% {opacity: 0; transform: rotate(90deg)}
            // 100% {opacity: 0;}
        }
    .el-card {
        z-index: 2;
        background: none;
        position: absolute;
        box-shadow: 0 0 0 0;
        border: 0;
        top: 30%;
        right: 10%;
        font-family: 'PingFangSC-Regular';
        .el-form {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .el-input__prefix {
            color: #fff;
        }
        .el-button {
            border-radius: 15px;
            background: -webkit-gradient(linear, left top, right top, from(#1EDBFF), to(#0DB5FF));
            letter-spacing: 5px;
            width: 100%;
            border: 0;
        }
        .el-input__inner {

            border: 0;
            border-bottom: 1px solid #fff;
            border-radius: 0;
            color: #fff;
            padding: 0 42px;
            background-color: transparent;
        }
    }

    .head-title {
        width: 100%;
        color: #ffffff;
        font-size: 48px;
        text-align: center;
    }

    .el-checkbox {
        color: #ffffff;
    }
    .el-form-item {
        width: 100%;
        .el-input {
            width: 100% !important;
        }
    }
    .el-form-item__content {
        text-align: left;
    }
}
</style>
